package org.jeecg.modules.wlhy.hytaxsalesinvoice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.third.dongjiang.tax.system.constant.TaxConstant;
import org.jeecg.modules.third.dongjiang.tax.system.dto.ApiBaseResDTO;
import org.jeecg.modules.third.dongjiang.tax.system.dto.req.invoice.SalesInvoiceDTO;
import org.jeecg.modules.third.dongjiang.tax.system.service.DongjiangTaxSystemService;
import org.jeecg.modules.wlhy.hytaxsalesinvoice.entity.HyTaxSalesinvoice;
import org.jeecg.modules.wlhy.hytaxsalesinvoice.mapper.HyTaxSalesinvoiceMapper;
import org.jeecg.modules.wlhy.hytaxsalesinvoice.service.IHyTaxSalesinvoiceService;
import org.jeecg.modules.wlhy.invoice.entity.HyInvoice;
import org.jeecg.modules.wlhy.invoice.service.IHyInvoiceService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: hy_tax_salesinvoice
 * @Author: duanlikao
 * @Date:   2021-11-22
 * @Version: V1.0
 */
@Service
public class HyTaxSalesinvoiceServiceImpl extends ServiceImpl<HyTaxSalesinvoiceMapper, HyTaxSalesinvoice> implements IHyTaxSalesinvoiceService {

    @Autowired
    private DongjiangTaxSystemService dongjiangTaxSystemService;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;
    @Autowired
    private IHyInvoiceService invoiceService;

    @Override
    public void  saveTaxSalesinvoice(HyInvoice hyInvoice){

        HyInvoice invoice = invoiceService.getById(hyInvoice.getId());
        if(invoice != null){
            List<HyTaxSalesinvoice> taxSalesinvoices = new ArrayList <>();

            String ids = invoice.getShippingIds();
            List <HyShippingNote> hyShippingNotes = hyShippingNoteService.listByIds(Arrays.asList(ids.split(",")));
            for (HyShippingNote hyShippingNote : hyShippingNotes) {
               if(StringUtils.equals("1" ,hyShippingNote.getTaxUploadStatus())){
                   HyTaxSalesinvoice  taxSalesinvoice = new HyTaxSalesinvoice();

                   taxSalesinvoice.setFpId(TaxConstant.API_TAX_CUSTOMER_SUBFIX + invoice.getId());
                   taxSalesinvoice.setGfmc(invoice.getCustomerName());
                   taxSalesinvoice.setGfsh(invoice.getCustomerCreditCode());
                   taxSalesinvoice.setJshjxx(invoice.getInvoiceTaxAmt());
                   taxSalesinvoice.setMxId(TaxConstant.API_TAX_CUSTOMER_SUBFIX +hyShippingNote.getOrderId());
                   taxSalesinvoice.setHwmc(hyShippingNote.getDescriptionOfGoods());

                   taxSalesinvoice.setFpdm(invoice.getInvoiceCode());
                   taxSalesinvoice.setFphm(invoice.getInvoiceHm());
                   taxSalesinvoice.setKpsj(invoice.getInvoiceDt());
                   taxSalesinvoice.setDduuid(TaxConstant.API_TAX_CUSTOMER_SUBFIX + hyShippingNote.getId());
                   taxSalesinvoice.setFplxId("1");
                   String  invoiceDtStr = DateUtils.dateTimeToString(invoice.getInvoiceDt()) ;
                   taxSalesinvoice.setFpssyf( invoiceDtStr.substring(0,7) );
                   taxSalesinvoice.setYxbz("Y");

                   taxSalesinvoices.add(taxSalesinvoice);
               }
            }
            if(CollectionUtils.isNotEmpty(taxSalesinvoices)){
                saveBatch(taxSalesinvoices);
            }
        }
    }





    @Override
    public ApiBaseResDTO uploadTax(String fpId) {
        QueryWrapper<HyTaxSalesinvoice> queryWrapper = new QueryWrapper();
        queryWrapper.eq("fp_id",fpId);
        queryWrapper.ne("upload_status","1");

        List<HyTaxSalesinvoice>   hyTaxSalesinvoices =  this.list(queryWrapper);
        if( CollectionUtils.isEmpty( hyTaxSalesinvoices) ){
            throw new JeecgBootException("未找到对应数据");
        }
        List<SalesInvoiceDTO> salesInvoiceDTOS = new ArrayList<>();
        for(HyTaxSalesinvoice taxSalesinvoice : hyTaxSalesinvoices){
            SalesInvoiceDTO salesInvoiceDTO = TaxSalesinvoiceTOSalesInvoiceDTO(taxSalesinvoice);
            salesInvoiceDTOS.add(salesInvoiceDTO);
        }

        ApiBaseResDTO reportResult  = dongjiangTaxSystemService.sendSalesInvoice(salesInvoiceDTOS);

        Integer code = reportResult != null && StringUtils.isNotEmpty(reportResult.getCode())? Integer.valueOf(reportResult.getCode()) : 0;
        if(code >= 100 && code < 200){
            for(HyTaxSalesinvoice taxSalesinvoiceDb : hyTaxSalesinvoices){
                taxSalesinvoiceDb.setUploadStatus( "1");
                taxSalesinvoiceDb.setUploadDate(new Date());
            }
            this.updateBatchById(hyTaxSalesinvoices);
        }else{
            throw new JeecgBootException("销项票上报失败:fpId " + fpId + " Desc: "+reportResult.getDesc() ) ;
        }
        return reportResult;
    }

    public static SalesInvoiceDTO TaxSalesinvoiceTOSalesInvoiceDTO(HyTaxSalesinvoice hyTaxSalesinvoice) {
        if (hyTaxSalesinvoice == null) {
            return null;
        }
        SalesInvoiceDTO salesInvoiceDTO = new SalesInvoiceDTO();

        salesInvoiceDTO.setF_id(hyTaxSalesinvoice.getFpId());
        salesInvoiceDTO.setGfmc(hyTaxSalesinvoice.getGfmc());
        salesInvoiceDTO.setGfsh(hyTaxSalesinvoice.getGfsh());
        salesInvoiceDTO.setJshjxx(
                String.format("%.2f",hyTaxSalesinvoice.getJshjxx())

        );
        salesInvoiceDTO.setM_id(
                TaxConstant.API_TAX_CUSTOMER_SUBFIX + get20UUID()
        );
        salesInvoiceDTO.setHwmc(hyTaxSalesinvoice.getHwmc());
        salesInvoiceDTO.setFpdm(hyTaxSalesinvoice.getFpdm());
        salesInvoiceDTO.setFphm(hyTaxSalesinvoice.getFphm());
        salesInvoiceDTO.setKpsj(
                DateUtils.date2Str(hyTaxSalesinvoice.getKpsj(),DateUtils.date_sdf.get())
        );
        salesInvoiceDTO.setDduuid(hyTaxSalesinvoice.getDduuid());
        salesInvoiceDTO.setFplx_id(hyTaxSalesinvoice.getFplxId());
        salesInvoiceDTO.setFpssyf(hyTaxSalesinvoice.getFpssyf());
        salesInvoiceDTO.setYxbz(hyTaxSalesinvoice.getYxbz());

        return salesInvoiceDTO;
    }

    public static String get20UUID(){

        UUID id=UUID.randomUUID();
        String[] idd=id.toString().split("-");
        return idd[0]+idd[1]+idd[2]+idd[3];
    }
}
